<?
require_once('class/restrictpage.php');
canConnectFor("ADMIN");
require_once('class/DeliveryRate.class.php');
require_once('../Connections/conn.php');
$delivery_Id=base64_decode($_GET["id"]);
$link= $_GET["id"];
$sqldv="SELECT *  FROM sme_delivery_type  WHERE  delivery_id=$delivery_Id";
$sqlmax="SELECT MAX(weight_end) AS max,COUNT(weight_end) AS count FROM sme_delivery_rate  WHERE  delivery_ids=$delivery_Id ";
$recordSql= mysql_query ( $sqldv, $conn );
$recordSqlmax= mysql_query ( $sqlmax, $conn );
$record=mysql_fetch_assoc($recordSql);
$recordmax=mysql_fetch_assoc($recordSqlmax);
$form=new delivery_rate();
if($form->checkParentRecord($delivery_Id))
{
	if(!empty($_POST['submit']))
{
	
	$form=new delivery_rate();
	$form->set($delivery_Id,$_POST['begin'],$_POST['end'],$_POST['price']);
	if($form->insert()){echo  "<meta http-equiv='refresh' content='0;URL=delivery_detail.php?id=$link'>";}

	}
	

}
else 	echo  "<meta http-equiv='refresh' content='0;URL=delivery.php'>";
	

?>

<!doctype html>
<html lang="th"><!-- InstanceBegin template="/Templates/temp.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<? 
$setac= array();
for($i=0;$i<9;$i++)
$setac[$i]="";
?>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet"  href="backstore_css.css" />
<!-- InstanceBeginEditable name="doctitle" -->

<title>หลังร้าน&gt;&gt; เพิ่มอัตราการขนส่ง</title>
<? $setac[4]="class=\"active\""; ?>
<!-- InstanceEndEditable -->
<link rel="icon" type="image/x-icon" id="favicon" href="img/faviconc.ico"/>
<!-- InstanceBeginEditable name="head" -->
<style type="text/css">
#contain #main #content table {
	color: #963;
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size: 14px;
	text-align: left;
}

.warning {
	color: #F00;
}
</style>
<script language="JavaScript" type="text/javascript">
function trim( text ){
    return text.replace(/^\s*/, "").replace(/\s*$/, "");
}
function chkform ( form )
{
	var i=0;

 var rmax=<?= $recordmax['max']+1;?>;
   if(trim(document.getElementById("end").value)=="")
  {document.getElementById("e").innerHTML="<img src=\"img/nav-logout.gif\"/>กรุณากรอกน้ำหนักสิ้นสุด";   i++;   }
   else if(isNaN(trim(document.getElementById("end").value))   ) 
  {document.getElementById("e").innerHTML="<img src=\"img/nav-logout.gif\"/>กรุณากรอกเป็นตัวเลข";   i++;   }
 else if(trim(document.getElementById("end").value)<=rmax)
  {document.getElementById("e").innerHTML="<img src=\"img/nav-logout.gif\"/>น้ำหนักสิ้นสุดต้องมากกว่าน้ำหนักเริ่มต้น";   i++;   }
    else document.getElementById("e").innerHTML="";
	
 
   if(trim(document.getElementById("price").value)=="")
  { document.getElementById("p").innerHTML="<img src=\"img/nav-logout.gif\"/>กรุณากรอกราคาต่อกล่อง";     i++;   }
  else if(isNaN(trim(document.getElementById("price").value))   ) 
  {document.getElementById("p").innerHTML="<img src=\"img/nav-logout.gif\"/>กรุณากรอกเป็นตัวเลข";   i++;   }
  else if(trim(document.getElementById("price").value)<0)
  { document.getElementById("p").innerHTML="<img src=\"img/nav-logout.gif\"/>กรุณากรอกตัวเลขที่ไม่ติดลบ";     i++;   }
   else document.getElementById("p").innerHTML="";
  

 if(i==0)
 return true;
 else
 return false;
 
}
//-->
</script>
<!-- InstanceEndEditable -->
</head>
<body>
<div id="contain">
<?php require("inc/menu_admin.php") ?>
<div id="main">
<ul id="submenu">
  <!-- InstanceBeginEditable name="EditRegion2" -->
<li><a href="delivery_form.php"><img src="img/add.gif" alt="" style="width: 16px; height: 16px;" />เพิ่มการขนส่งใหม่</a></li>
  <!-- InstanceEndEditable -->			
</ul>
<div id="content">
<div class="path_bar">
<!-- InstanceBeginEditable name="EditRegion6" --><img src="img/prefs.gif" style="margin-right: 10px;" /><a href="#">หลังร้าน</a> &gt;&gt; <img src="img/AdminShipping.gif" style="margin-right: 5px;" /> <a href="delivery.php">การขนส่ง &gt;&gt;</a><img src="img/AdminRangePrice.gif" alt="" style="margin-right: 5px;" /><a href="delivery_detail.php?id=<? echo  base64_encode($record["delivery_Id"]); ?>"> อัตราการขนส่ง<? echo $record["delivery_name"]; ?>&gt;&gt;</a><img src="img/add.gif" alt="" style="margin-right: 5px;" /> เพิ่มอัตราการขนส่งอันดับที่ <? echo $recordmax['count']+1; ?>.
<!-- InstanceEndEditable -->
</div>
<!-- InstanceBeginEditable name="EditRegion1" -->
<form method="post" name="form1" id="form1"  onsubmit="return chkform(this);"  >

  <table width="404" height="193"  >
    <tr bgcolor="#F0F0F0">
      <td align="right">น้ำหนักเริ่มต้น </td>
      <td><? if( $recordmax['max']!="")
	  { 
	  $beginv=$recordmax['max']+1;
	 echo $beginv;
	  }
	  else  {
		  $beginv=1;
		   echo $beginv;
	  }
	  ?>
       
         กรัม
           <input name="begin" type="hidden" id="begin" value="<?   echo $beginv; ?>" /></td>
         
    </tr>
    <tr bgcolor="#FBFBFB">
      <td align="right">น้ำหนักสิ้นสุด</td>
      <td>
        <input name="end" type="text" id="end" size="10" /> กรัม
      <p id="e"   class="warning"></p>  
      </td>
    </tr>
    <tr  bgcolor="#F0F0F0" >
      <td  align="right">ราคาต่อกล่อง</td>
      <td ><input name="price" type="text" id="price" size="10" />
      <p id="p"   class="warning"></p></td>
    </tr>
    <tr bgcolor="#F0F0F0">
      <td  align="center" bgcolor="#FFFFFF">&nbsp;</td>
      <td  align="center" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
    <tr bgcolor="#F0F0F0">
      <td colspan="2"  align="center"><input type="submit" name="submit" id="submit" value="บันทึก" />
&nbsp;
<input type="reset" name="reset" id="reset" value="ยกเลิก"  /></td>
    </tr>
  </table>
</form>
<br style="clear: both;"/>
<br style="clear: both;"/>
<!-- InstanceEndEditable -->
</div>
</div>
</div>
<?php require("inc/footer_admin.php") ?>
</body>
<!-- InstanceEnd --></html>